/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        bool isValidBST(TreeNode* root) {
            return dfs(root);
        }
        long prev = LONG_MIN;//全局变量提高效率
        bool dfs(TreeNode* root){
            if(root == nullptr) return true;
            bool ret2 = dfs(root->left);
            if(ret2 == false) return false; //剪枝提高效率
            bool ret1 = false;
            if(root->val > prev)
                ret1 = true;
            if(ret1 == false ) return false; //剪枝提高效率
            prev = root->val;
            bool ret3 = dfs(root->right);
            return ret2&&ret1&&ret3;
    
        }
    };